home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’95 / Venus / builtin.h next >
Encoding:
Text File  |  1993-10-04  |  3.9 KB  |  158 lines  |  [TEXT/EDIT]

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1988, 1992 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /*
  21.   arithmetic, etc. functions on built in types
  22. */
  23.  
  24. #pragma once
  25. #ifndef _builtin_h
  26. #ifdef __GNUG__
  27. #pragma interface
  28. #endif
  29. #define _builtin_h 1
  30.  
  31. #pragma template_access static
  32. #include <std.h>
  33. #include <math.h>
  34.  
  35. #ifdef __GNUG__
  36. #define _VOLATILE_VOID volatile void
  37. #else
  38. #define _VOLATILE_VOID void
  39. #endif
  40.  
  41. typedef void (*one_arg_error_handler_t)(const char*);
  42. typedef void (*two_arg_error_handler_t)(const char*, const char*);
  43.  
  44. long         gcd(long, long);
  45. long         lg(unsigned long); 
  46. double       pow(double, long);
  47. long         pow(long, long);
  48.  
  49. double       start_timer();
  50. double       return_elapsed_time(double last_time = 0.0);
  51.  
  52. char*        dtoa(double x, char cvt = 'g', int width = 0, int prec = 6);
  53.  
  54. char*        chr(char ch, int width = 0);
  55. char*        str(const char* s, int width = 0);
  56.  
  57. unsigned int hashpjw(const char*);
  58. unsigned int multiplicativehash(int);
  59. unsigned int foldhash(double);
  60.  
  61. extern _VOLATILE_VOID default_one_arg_error_handler(const char*);
  62. extern _VOLATILE_VOID default_two_arg_error_handler(const char*, const char*);
  63.  
  64. extern two_arg_error_handler_t lib_error_handler;
  65.  
  66. extern two_arg_error_handler_t 
  67.        set_lib_error_handler(two_arg_error_handler_t f);
  68.  
  69. template<class T> inline T abs(T x) { return x < 0 ? -x : x; }
  70.  
  71. //double abs(double arg);
  72. //float abs(float arg);
  73. //short abs(short arg);
  74. //long abs(long arg);
  75. int sign(long arg);
  76. int sign(double arg);
  77. long sqr(long arg);
  78. double sqr(double arg);
  79. int even(long arg);
  80. int odd(long arg);
  81. long lcm(long x, long y);
  82. void setbit(long& x, long b);
  83. void clearbit(long& x, long b);
  84. int testbit(long x, long b);
  85.  
  86. inline int sign(long arg)
  87. {
  88.   return (arg == 0) ? 0 : ( (arg > 0) ? 1 : -1 );
  89. }
  90.  
  91. inline int sign(double arg)
  92. {
  93.   return (arg == 0.0) ? 0 : ( (arg > 0.0) ? 1 : -1 );
  94. }
  95.  
  96. inline long sqr(long arg)
  97. {
  98.   return arg * arg;
  99. }
  100.  
  101. inline double sqr(double arg)
  102. {
  103.   return arg * arg;
  104. }
  105.  
  106. inline int even(long arg)
  107. {
  108.   return !(arg & 1);
  109. }
  110.  
  111. inline int odd(long arg)
  112. {
  113.   return (arg & 1);
  114. }
  115.  
  116. inline long lcm(long x, long y)
  117. {
  118.   return x / gcd(x, y) * y;
  119. }
  120.  
  121. inline void setbit(long& x, long b)
  122. {
  123.   x |= (1 << b);
  124. }
  125.  
  126. inline void clearbit(long& x, long b)
  127. {
  128.   x &= ~(1 << b);
  129. }
  130.  
  131. inline int testbit(long x, long b)
  132. {
  133.   return ((x & (1 << b)) != 0);
  134. }
  135.  
  136. template<class T> inline T min(T a, T b) { return a < b ? a : b; }
  137. template<class T> inline T max(T a, T b) { return a > b ? a : b; }
  138.  
  139. inline int min(int a, int b) {return (a < b)?a:b;}
  140. inline int max(int a, int b) {return (a > b)?a:b;}
  141. #if 0
  142. inline char max(char a, char b) { return (a > b)?a:b;}
  143. inline unsigned char max(unsigned char a, unsigned char b) {return (a>b)?a:b;}
  144.  
  145. inline short max(short a, short b) {return (a > b) ?a:b;}
  146. inline unsigned short max(unsigned short a, unsigned short b) {return (a > b)?a:b;}
  147.  
  148. inline unsigned int max(unsigned int a, unsigned int b) {return (a > b)?a:b;}
  149.  
  150. inline long max(long a, long b) {return (a > b)?a:b;}
  151. inline unsigned long max(unsigned long a, unsigned long b) {return (a>b)?a:b;}
  152.  
  153. inline float max(float a, float b) {return (a > b)?a:b;}
  154.  
  155. inline double max(double a, double b) {return (a > b)?a:b;}
  156. #endif
  157. #endif
  158.